package org.lxy.controller;


import cn.hutool.core.bean.BeanUtil;
import org.apache.commons.lang.StringUtils;
import org.lxy.config.OSSClientConfig;
import org.lxy.domain.InfomationEntity;
import org.lxy.domain.UserInfoEntity;
import org.lxy.domain.constant.CoreConstant;
import org.lxy.service.InfomationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/infomation")
public class InfomationController {
    @Autowired
    private InfomationService infomationService;
    @Autowired
    private OSSClientConfig ossClientUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView list(InfomationEntity infomationEntity) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list", infomationService.listByMap(BeanUtil.beanToMap(infomationEntity, true, true)));
        return modelAndView;
    }

    @RequestMapping("/operation")
    public ModelAndView operation(Long id, @RequestParam(defaultValue = CoreConstant.LIST) String op) {
        ModelAndView modelAndView = new ModelAndView();
        if (CoreConstant.LIST.equals(op)) {
            List<InfomationEntity> list = infomationService.list();
            modelAndView.addObject("list", list);
            modelAndView.setViewName("/zixun/zixunlist");
        } else if (CoreConstant.ADD.equals(op)) {
            InfomationEntity entity = new InfomationEntity();
            modelAndView.addObject("entitys", entity);
            modelAndView.setViewName("/zixun/zixunEdit");
        } else if (CoreConstant.EDIT.equals(op)) {
            InfomationEntity entity = infomationService.getById(id);
            modelAndView.addObject("entitys", entity);
            modelAndView.setViewName("/zixun/zixunEdit");
        }
        return modelAndView;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(String ids) {
        return infomationService.removeByIds(Arrays.asList(ids.split(","))) ? 1 : 0;
    }

    @RequestMapping("/edit")
    public void edit(MultipartFile tupian,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        String title = "";
        String jianjie = "";
        String neirong = "";
        int shuliang = 0;
        int id = 0;
        String fname = "";
        String xiangqing = "";
        String s = ossClientUtil.uploadFile2OSS(tupian);
        title = request.getParameter("title");
        jianjie = request.getParameter("jianjie");
        neirong = request.getParameter("neirong");
        id = Integer.parseInt(request.getParameter("id"));
        InfomationEntity entity = new InfomationEntity();
        UserInfoEntity account = (UserInfoEntity) request.getSession().getAttribute("Account");
        PrintWriter out = response.getWriter();
        if (account == null) {
            out.write("<script>window.top.location.href='Login.jsp';</script>");
            return;
        }
        if (id > 0) {
            entity = infomationService.getById(id);
        } else {
            entity.setId(0);
            entity.setStatus("0");
        }
        entity.setInfo(jianjie);
        entity.setContent(neirong);
        entity.setTitle(title);
        if (StringUtils.isNotBlank(s)){
            entity.setImg(s);
        }
        boolean tag = false;
        if (entity.getId() > 0) {
            tag = infomationService.updateById(entity);
        } else {
            tag = infomationService.save(entity);
        }
        if (tag) {
            out.write(
                    "<script>alert('操作成功!');window.parent.location.href='/infomation/operation?op=List"
                            + "';</script>");

        } else {
            out.write("<script>alert('操作失败!');</script>");
        }
    }

    /*   *//**
     * 信息
     *//*
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		InfomationEntity infomation = infomationService.getById(id);

        return R.ok().put("infomation", infomation);
    }

    *//**
     * 保存
     *//*
    @RequestMapping("/save")
    public R save(@RequestBody InfomationEntity infomation){
		infomationService.save(infomation);

        return R.ok();
    }

    *//**
     * 修改
     *//*
    @RequestMapping("/update")
    public R update(@RequestBody InfomationEntity infomation){
		infomationService.updateById(infomation);

        return R.ok();
    }

    *//**
     * 删除
     *//*
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		infomationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
*/
}
